import java.awt.Font;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

/**
 * 
 * System's Main Window
 *
 */

public class MainScreen implements ActionListener 
{

	private static final long serialVersionUID = 1L;
	
	/** array of strings */
	public String [] details;
	/** array of strings */
	public String[] res;
	/** array of strings */
	public String[] res1;
	/** array of strings */
	public String[] res2;
	/** holds temporary result from server */
	public String temp;
	/** holds temporary result from server */
	public String temp2;
	/** array of strings */
	public String[] searchData3;
	/** result array of authors of strings */
	public String[] Ares;
	public JButton jSearchRButton;
	/** flag that indicate that only authors result is needed */
	public boolean justAflag =false;
	/** flag that an error is occurred */
	public boolean errorflag =false;
	/** Review Count Label */
	private JLabel ReviewCountLabel = null;
	/** Review Count Text */
	public JTextField ReviewCountTxt = null;
	/** String for Review Count result from server */
	public String[] ReviewCount;	
	/** Thread that runs repeatedly: retreives Review Count result from server */
	public Thread CountReviewsThread;	
	/** String for Book's Subject data, received from server, for UpdateBookSubject use*/
	public String[] SubjectsData;
	/** Title Label */
	private JLabel mainTitle = null;
	/** Navigation Button - review Mangagement Screen (Available For librarian and Manager only)*/
	private JButton reviewManagement = null;
	/** Navigation Button - book Mangagement Screen (Available For librarian and Manager only)*/
	private JButton bookManagement = null;
	/** Navigation Button - log out of the system */
	private JButton logout1 = null;
	/** window panel */
	public JPanel panel = new JPanel();  //  @jve:decl-index=0:visual-constraint="37,-26"
	/** Navigation Button - add review Screen (Available for all) */
	private JButton addReview = null;
	/** Navigation Button - shopping cart Screen (Available for all) */
	private JButton goToCart = null;
	/** Navigation Button - search book Screen (Available for all) */
	private JButton searchBook = null;
	/** Navigation Button - Book Report Screen (Available for Manager only)*/
	private JButton bookReport = null;
	/** Navigation Button - Reader Report Screen (Available for Manager only) */
	private JButton readerReport = null;
	/** Navigation Button - Personal Data Management Screen (Available for Manager only) */
	private JButton personalManagement = null;
	/** Navigation Button - purchased books Screen, actual file download (Available for all) */
	private JButton myBooks = null;

	
	
	/**
	 * This is the default constructor
	 */
	public MainScreen() {
		super();
		initialize();

		
	}

	/**
	 * This method initializes this, set buttons by authorization
	 * 
	 * @return void
	 */
public void initialize() {
		
		panel.setLayout(null);
		panel.add(getMainTitle(), null);
		panel.add(getLogout1(), null);
		panel.add(getAddReview(), null);
		panel.add(getGoToCart(), null);
		panel.add(getSearchBook(), null);
		if(Main.gusers.authorozations==2 || Main.gusers.authorozations==3)
		{
			panel.add(getReviewManagement(), null);
			panel.add(getBookManagement(), null);
		}
		if(Main.gusers.authorozations==3)
		{
			panel.add(getBookReport(), null);
			panel.add(getReaderReport(), null);
			panel.add(getPersonalManagement(), null);
		}
		panel.setVisible(false);
		panel.setEnabled(false);
		panel.add(getMyBooks(), null);
		panel.add(getJSearchRButton(), null);
	}

	/**
	 * This method initializes the Thread that retreives Reviews count, and runs it
	 * 
	 * @return void
	 */		
		public void SetReviewsCount()
		{
			panel.add(getReviewCountLabel(), null);
			CountReviewsThread = new CountReviews(this);
			CountReviewsThread.start();			
		}
	/**
	 * This method initializes the Lable : "Unposted Reviews:"
	 * 
	 * @return void
	 */
	private JLabel getReviewCountLabel() 
	{
		if (ReviewCountLabel == null) {
			ReviewCountLabel = new JLabel("Unposted Reviews:");
			ReviewCountLabel.setFont(new Font("David",Font.ITALIC,18));
			ReviewCountLabel.setBounds(new Rectangle(200, 284, 150, 44));
		}
		return ReviewCountLabel;
	}
	/**
	 *  Initialize Main title label
	 */
	private JLabel getMainTitle() 
	{
		if (mainTitle == null) {
			mainTitle = new JLabel("Good Reading");
			mainTitle.setFont(new Font("David",Font.ITALIC,30));
			mainTitle.setBounds(new Rectangle(120, 38, 183, 44));
		}
		return mainTitle;
	}

	/**
	 * initialize ReviewManagement and add listeners
	 * @return JButton
	 */
	private JButton getReviewManagement() 
	{
		if (reviewManagement == null) {
			reviewManagement = new JButton("Review Management");
			reviewManagement.setBounds(new Rectangle(31, 288, 160, 34));
			reviewManagement.addActionListener(this);
			
		}
		return reviewManagement;
	}
	/**
	 * initialize BookManagement and add listeners
	 * @return JButton
	 */

	private JButton getBookManagement()
	{
		if (bookManagement == null) {
			bookManagement = new JButton("Book Management");
			bookManagement.setBounds(new Rectangle(32, 241, 359, 33));
			bookManagement.addActionListener(this);
		}
		return bookManagement;
	}
	

	/**
	 * This method initializes logout1 and add listeners
	 * 	
	 * @return JButton	
	 */
	JButton getLogout1() {
		if (logout1 == null) {
			logout1 = new JButton("Logout");
			logout1.setBounds(new Rectangle(338, 8, 74, 24));
			logout1.addActionListener(this);
		}
		return logout1;
	}

	/**
	 * This method initializes addReview and add listeners
	 * 	
	 * @return JButton	
	 */
	private JButton getAddReview() {
		if (addReview == null) {
			addReview = new JButton("Book Review");
			addReview.setBounds(new Rectangle(33, 150, 180, 32));
			addReview.addActionListener(this);
		}
		return addReview;
	}

	/**
	 * This method initializes goToCart	and add listeners
	 * 	
	 * @return JButton	
	 */
	private JButton getGoToCart() {
		if (goToCart == null) {
			goToCart = new JButton("My Cart");
			goToCart.setBounds(new Rectangle(33, 194, 179, 32));
			goToCart.addActionListener(this);
		}
		return goToCart;
	}

	/**
	 * This method initializes searchBook and add listeners	
	 * 	
	 * @return JButton	
	 */
	private JButton getSearchBook() {
		if (searchBook == null) {
			searchBook = new JButton("Search Book");
			searchBook.setBounds(new Rectangle(32, 105, 358, 33));
			searchBook.addActionListener(this);
		}
		return searchBook;
	}

	/**
	 * This method initializes bookReport and add listeners
	 * 	
	 * @return JButton	
	 */
	private JButton getBookReport() {
		if (bookReport == null) {
			bookReport = new JButton("Book Report");
			bookReport.setBounds(new Rectangle(30, 334, 360, 34));
			bookReport.addActionListener(this);
		}
		return bookReport;
	}

	/**
	 * This method initializes readerReport	and add listeners
	 * 	
	 * @return JButton	
	 */
	private JButton getReaderReport() {
		if (readerReport == null) {
			readerReport = new JButton("Reader Report");
			readerReport.setBounds(new Rectangle(31, 384, 358, 32));
			readerReport.addActionListener(this);
		}
		return readerReport;
	}

	/**
	 * This method initializes personalManagement and add listeners	
	 * 	
	 * @return JButton	
	 */
	private JButton getPersonalManagement() {
		if (personalManagement == null) {
			personalManagement = new JButton("Personal Data Management");
			personalManagement.setBounds(new Rectangle(32, 430, 356, 33));
			personalManagement.addActionListener(this);
		}
		return personalManagement;
	}
	/**
	 * when one of the buttons is pressed, this method is called 
	 */
	public void actionPerformed(ActionEvent ae)
	{
	
		if(ae.getSource()==bookManagement)
		{
			panel.setEnabled(false);
			panel.setVisible(false);
			Main.log.frame.setSize(530, 250);
			Main.log.frame.add(Main.bookManagement.panel);
			Main.bookManagement.panel.setEnabled(true);
			Main.bookManagement.panel.setVisible(true);
		}

		if(ae.getSource()==reviewManagement)
		{
			boolean post = true;
			Main.reviewManagement = new ReviewManagement();
			if (Main.reviewManagement.ReviewCombo==null)
				JOptionPane.showMessageDialog(null,"No Reviews to Confirm...","Database message",JOptionPane.INFORMATION_MESSAGE);
			else
			{
				panel.setEnabled(false);
				panel.setVisible(false);			
				Main.log.frame.setSize(600,450);
				Main.log.frame.add(Main.reviewManagement.panel);
				Main.reviewManagement.panel.setEnabled(true);
				Main.reviewManagement.panel.setVisible(true);
			}
			
		}
		if(ae.getSource()==getAddReview())
		{
			panel.setEnabled(false);
			panel.setVisible(false);
			Main.log.frame.setSize(600,450);
			Main.addReviewGui=new AddReviewGui();
			Main.log.frame.add(Main.addReviewGui.panel);
			Main.addReviewGui.panel.setEnabled(true);
			Main.addReviewGui.panel.setVisible(true);
		}

		
		if(ae.getSource()==getLogout1())
		{
			
			if(JOptionPane.showConfirmDialog(null, "Are you sure you want to logout ?","Confirmation", 0) == 0)
			{
				panel.setEnabled(false);
				panel.setVisible(false);
				Main.log.loginData=new String[3]; 
				Main.log.loginData[0]=Main.gusers.id;
				Main.log.loginData[1]="logout";
				Main.log.loginData[2]=Main.gusers.id;
				Main.log.connection.accept("logout"); 
				Main.log.flag=true;
				while(Main.log.flag==false);
				Main.log.flag=false;
				Main.log.user.setText("");
				Main.log.pass.setText("");
				Main.log.frame.setSize(650,550);
				Main.log.panel.setEnabled(true);
				Main.log.panel.setVisible(true);
			}
			
		}
	if(ae.getSource()==getSearchBook())
		{	
			panel.setEnabled(false);
			panel.setVisible(false);
			Main.searchBookC = new SearchBookCONTROL();
			Main.searchBookG = new SearchBookGUI();
			Main.log.frame.setSize(730, 560);
			Main.log.frame.add(Main.searchBookG);
			Main.searchBookG.setEnabled(true);
			Main.searchBookG.setVisible(true);
			
		}
	if(ae.getSource()==getJSearchRButton())
	{
		panel.setEnabled(false);
		panel.setVisible(false);
		Main.log.frame.setSize(720, 500);
		Main.searchReviewG=new SearchReviewG();
		Main.searchReviewC=new SearchReviewC();
		Main.log.frame.add(Main.searchReviewG);
		Main.searchReviewG.setEnabled(true);
		Main.searchReviewG.setVisible(true);
	}

	
		if(ae.getSource()==getGoToCart()) 
		{
			Main.cartgui=new CartGui(); 
			panel.setEnabled(false);
			panel.setVisible(false);
			Main.log.frame.setSize(560, 425);
			Main.log.frame.add(Main.cartgui.panel);
			Main.cartgui.panel.setEnabled(true);
			Main.cartgui.panel.setVisible(true);
			
		}
		if(ae.getSource()==getBookReport())
		{
			Main.brc=new BookReportControl(true);
			Main.log.frame.setVisible(false);
			Main.log.frame.setEnabled(false);
			Main.brc.lload();
		}
		if(ae.getSource()==getReaderReport())
		{
			Main.pdmc=new PersonalDataManagmentCONTROL();
			Main.pdmc.lsreachUserug(false);
			Main.log.frame.setSize(900, 450);
			panel.setEnabled(false);
			panel.setVisible(false);
		}
		if(ae.getSource()==getPersonalManagement())
		{
			Main.pdmc=new PersonalDataManagmentCONTROL();
			Main.pdmc.lsreachUserug(true);
			Main.log.frame.setSize(702, 600);
			panel.setEnabled(false);
			panel.setVisible(false);
		}
				
	}
	/**
	 * get String value forward it to data-base to get the selected book, by the user, details , then get the book details
	 * and set it res(array of strings).
	 * @param String st
	 * 
	 * @return void
	 */
	public void d_Retrieve_Set(String st)
	{
		details = new String [3];
		String temp;
		String temp2="";
		String [] tmpV;
		temp2 = st;
		tmpV=temp2.split("\\-");
		temp2=tmpV[0];
		tmpV=temp2.split("\\:");
		details[0]=Main.gusers.id;
		details[1]="retrieve_details";
		details[2]= tmpV[0];
		Main.log.flag=false;
		Main.log.connection.accept("getdetails");
		
		while(Main.log.flag==false);
		Main.log.flag=false;
		temp=ChatClient.getRs();
		if (!Main.welcomeScreen.justAflag)
		{
			res=temp.split("\\*");
			temp=res[10];
		}
		res1=temp.split("\\-");
		
		details = new String [3];
		details[0]=Main.gusers.id;
		details[1]="retrieve_language";
		details[2]= tmpV[0];
		
		Main.log.flag=false;
		Main.log.connection.accept("getlanguage");
		
		while(Main.log.flag==false);
		Main.log.flag=false;
		temp=ChatClient.getRs();
		res2=temp.split("\\*");
	}
	/**
	 * get the types of the book fields existed in data-base and set it in Ares()array of strings)
	 * @param String st
	 * 
	 * @return void
	 */
	public void  o_GetFields() 
	{
		String temp;	 
		searchData3 = new String[3];
		searchData3[0]=Main.gusers.id;

			searchData3[2]=(String)Main.addNBookG.SubjectCombo2.getSelectedItem();
			searchData3[1]="YgetFieldsSubject";
			
		Main.log.connection.accept(searchData3[1]);
		Main.log.flag=false;
		while(Main.log.flag==false);
		Main.log.flag=false;
		temp=ChatClient.getRs();
		Ares=temp.split("\\*");			
	}
	public JButton getJSearchRButton() {
		if (jSearchRButton == null) {
			jSearchRButton = new JButton();
			jSearchRButton.setBounds(new Rectangle(222, 150, 168, 31));
			jSearchRButton.setText("Search Review");
			jSearchRButton.addActionListener(this);
		}
		return jSearchRButton;
	}
	
	/**
	 * This method initializes myBooksand add listeners
	 * 	
	 * @return JButton	
	 */
	private JButton getMyBooks() {
		if (myBooks == null) {
			myBooks = new JButton("My Books");
			myBooks.setBounds(new Rectangle(222, 193, 168, 33));
			myBooks.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) 
				{
					Main.myBooksControl=new MyBooksControl(); 
					Main.myBooksGui=new MyBooksGui();
					panel.setEnabled(false);
					panel.setVisible(false);
					Main.log.frame.setSize(620, 260);
					Main.log.frame.add(Main.myBooksGui.panel);
					Main.myBooksGui.panel.setEnabled(true);
					Main.myBooksGui.panel.setVisible(true);
				}
			});
		}
		return myBooks;
	}
}